package per.zzzfwd.query;

public class ComparisonPredicate implements Predicate, Renderable {

    private ComparisonOperator comparisonOperator;
    private Expression leftExpression;
    private Expression rightExpression;

    public ComparisonPredicate(
        ComparisonOperator comparisonOperator,
        Expression leftExpression,
        Expression rightExpression) {
        this.comparisonOperator = comparisonOperator;
        this.leftExpression = leftExpression;
        this.rightExpression = rightExpression;
    }

    public ComparisonOperator getComparisonOperator() {
        return comparisonOperator;
    }

    public Expression getLeftExpression() {
        return leftExpression;
    }

    public Expression getRightExpression() {
        return rightExpression;
    }

    @Override
    public String render() {
        return ((Renderable) leftExpression).render()
            + ":"
            + comparisonOperator.begin()
            + ((Renderable) rightExpression).render()
            + comparisonOperator.end();
    }

    public enum ComparisonOperator {
        EQUAL {
            public String begin() {
                return "";
            }

            public String end() {
                return "";
            }
        },
        NOT_EQUAL {
            public String begin() {
                return "";
            }

            public String end() {
                return "";
            }
        },
        LESS_THAN {
            public String begin() {
                return "{* TO ";
            }

            public String end() {
                return "}";
            }
        },
        LESS_THAN_OR_EQUAL {
            public String begin() {
                return "{* TO ";
            }

            public String end() {
                return "]";
            }
        },
        GREATER_THAN {
            public String begin() {
                return "{";
            }

            public String end() {
                return " TO *}";
            }
        },
        GREATER_THAN_OR_EQUAL {
            public String begin() {
                return "[";
            }

            public String end() {
                return " TO *}";
            }
        };

        public abstract String begin();

        public abstract String end();
    }
}
